﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace bts_webapp
{
    public partial class UserChangePassword : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.txtUserId.Text = Common.CurrentUser.User_Id;
            this.txtUsername.Text = Common.CurrentUser.User_Full_Name;
            this.txtAddress.Text = Common.CurrentUser.User_Address;
        }

        protected void btChange_Click(object sender, EventArgs e)
        {
            if (this.txtOldPass.Text.Trim() != Common.CurrentUser.User_Pass)
            {
                this.litStatus.Text = string.Format("<font color='red'>{0}</font>", "Password cu khong dung.");
                return;
            }
            
            if (this.txtNewPass.Text.Trim() != this.txtNewPass2.Text.Trim())
            {
                this.litStatus.Text = string.Format("<font color='red'>{0}</font>", "Password moi khong khop.");
                return;
            }
            if (this.txtNewPass.Text.Trim().Length < 5)
            {
                this.litStatus.Text = string.Format("<font color='red'>{0}</font>", "Password phai lon hon hoac bang 5 ki tu.");
                return;
            }
            
            //save changes
            var user = this.DbContext.VIBA_Users.FirstOrDefault(x => x.User_Id == Common.CurrentUser.User_Id);
            if (user != null)
            {
                user.User_Full_Name = this.txtUsername.Text.Trim();
                user.User_Pass = this.txtNewPass.Text.Trim();
                user.User_Address = this.txtAddress.Text.Trim();

                this.DbContext.SubmitChanges();
            }
            //
            Response.Redirect("~/Account/Login.aspx", true);
        }
    }
}